//
//  CardCell.swift
//  YueChat
//
//  Created by IMac on 2024/9/23.
//

import UIKit

class CardCell: BaseChatCell {

    
    @IBOutlet weak var dappImg: UIImageView!
    @IBOutlet weak var descLab: UILabel!
    @IBOutlet weak var remark: UILabel! //个人名片
    @IBOutlet weak var cardName: UILabel!
    @IBOutlet weak var cardIcon: UIImageView!
    
    @IBOutlet weak var cardViewHeight: NSLayoutConstraint!
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    override func baseCellHeight(message: DMCCMessage, chatType: Bool, mulSelect: Bool, isMySend: Bool) {
        message.msgCellWidth = 264
        message.msgCellHeight = 120

        
        let cardContent = message.content as! OsnCardMessageContent
        if cardContent.theme.count > 0 {
            if cardContent.cardType == "litapp" && cardContent.theme.count > 0 {
                message.msgCellHeight = 120 + 200
            }
        }

    }
    
    override func setMessage(_ message: DMCCMessage) {
        super.setMessage(message)
  
        if let cardContent = message.content as? OsnCardMessageContent {
            if cardContent.portrait.count == 0 {
                cardContent.portrait = ""
            }
            
            self.cardIcon.sd_setImage(with: URL(string: cardContent.portrait), placeholderImage: SDImageDefault)
            self.cardName.text = cardContent.displayName.count > 0 ?  cardContent.displayName : cardContent.name
            self.remark.text = KLocalizedString("CardTypePerson")
            self.descLab.isHidden = true
            self.cardViewHeight.constant = 100.0
            self.dappImg.isHidden = true
            
            if cardContent.cardType == "litapp" {
                if cardContent.displayName.count > 0 {
                    self.descLab.text = cardContent.displayName
                    self.descLab.isHidden = false
                }
                
                if cardContent.theme.count > 0 {
                    self.dappImg.sd_setImage(with: URL(string: cardContent.theme), placeholderImage: UIImage(named: "failPhoto"))
                    self.cardViewHeight.constant = 300.0
                    self.dappImg.isHidden = false
                }
                
                self.cardName.text = cardContent.name
                self.remark.text = KLocalizedString("CardTypeDapp")
            } else if cardContent.cardType == "group" {
                self.cardName.text = cardContent.name
                self.remark.text = KLocalizedString("CardTypeGroup")
            }
        }
    }
    
}
